package com.leo.foodmaster.admin.system.mapper;

import com.leo.foodmaster.admin.system.dto.PermissionRolesDTO;
import com.leo.foodmaster.admin.system.entity.Permission;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.leo.foodmaster.admin.system.entity.Router;
import com.leo.foodmaster.commons.entity.TreeSelect;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 菜单权限表 Mapper 接口
 * </p>
 *
 * @author Leo
 * @since 2021-06-18
 */
public interface PermissionMapper extends BaseMapper<Permission> {

    /**
     * 查询权限角色关系列表
     * @return List<PermissionRolesDTO>
     */
    List<PermissionRolesDTO> listPermissionRoles();

    /**
     * 根据登录用户Id查询用户权限
     * @param userId 用户Id
     * @return 用户权限列表
     */
    @Select("SELECT c.perms " +
            "FROM sys_user_role AS a " +
            "JOIN sys_role_permission AS b " +
            "ON a.role_id = b.role_id " +
            "JOIN sys_permission AS c " +
            "ON b.permission_id = c.id " +
            "WHERE a.user_id = #{userId} AND c.type = 'F' AND c.`status` = '0' ")
    List<String> getPermissionsByUserId(Long userId);

    /**
     * 根据用户id和parentId查询路由
     * @param userId 用户Id
     * @param parentId 父级Id
     * @return 路由列表
     */
    @Select("SELECT a.id, a.`name`, a.title,  a.path, a.component, a.is_frame, a.is_cache, a.icon, " +
            "CASE WHEN a.visible = '0' THEN 0 ELSE 1 " +
            "END AS hidden, " +
            "CASE WHEN a.is_cache = 1 THEN 0 ELSE 1 " +
            "END AS noCache " +
            "FROM sys_permission AS a " +
            "JOIN sys_role_permission AS b " +
            "ON a.id = b.permission_id " +
            "JOIN sys_user_role AS c " +
            "ON b.role_id = c.role_id " +
            "WHERE c.user_id = #{userId} AND a.`status` = '0' AND a.type IN ('M', 'C') AND parent_id = #{parentId} " +
            "ORDER BY a.order_num ASC")
    List<Router> getRoutersByUserId(@Param("userId") Long userId, @Param("parentId")Long parentId);

    /**
     * 根据用户Id
     * @param userId 用户Id
     * @param parentId 父Id
     * @return List<TreeSelect>
     */
    @Select("<script>" +
            "SELECT a.id, a.title AS label " +
            "FROM sys_permission AS a " +

            "<if test='userId != null '>" +
            "JOIN sys_role_permission AS b " +
            "ON a.id = b.permission_id " +
            "JOIN sys_user_role AS c " +
            "ON c.role_id = b.role_id " +
            "</if>" +

            "WHERE a.parent_id = #{parentId} " +

            "<if test='userId != null '>" +
            "AND c.user_id = #{userId} " +
            "</if>" +

            "ORDER BY a.order_num asc " +
            "</script>")
    List<TreeSelect> getMenuTreeByUserId(Long userId, Long parentId);

    /**
     * 根据角色Id查询菜单Id列表
     * @param roleId 角色Id
     * @return List<Long>
     */
    @Select("SELECT permission_id " +
            "FROM sys_role_permission " +
            "WHERE role_id = #{roleId} ")
    List<Long> permissionIdsByRoleId(Long roleId);
}
